<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Coin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CoinController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Coin(), function (Grid $grid) {
            // 在工具栏添加操作按钮，用于上传Excel文件及下载导入模板
            $grid->tools(function (Grid\Tools $tools) {
                $tools->append(\Dcat\Admin\Widgets\Modal::make()
                    // 大号弹窗
                    ->lg()
                    // 弹窗标题
                    ->title('导入硬币数据')
                    // 按钮
                    ->button('<button class="btn btn-primary"><i class="feather icon-upload"></i> 导入硬币</button>')
                    // 弹窗内容
                    ->body(\App\Admin\Forms\CoinImportForm::make()));
                // 下载导入模板
//                $tools->append(\App\Admin\Actions\DownloadCoinTemplate::make());
            });

            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('type')->display(function ($type) {
                return \App\Models\Coin::TYPE[$type];
            });
//            $grid->column('longitude');
//            $grid->column('latitude');
            $grid->column('address', '地址');
            $grid->column('status')->bool();
            $grid->column('is_unlock')->bool();
            $grid->column('updated_at')->sortable();

            $grid->column('二维码')->modal(function ($modal) {
                $modal->title('二维码');//模态框标题
//                $modal->icon('feather icon-eye');//按钮图标

                $code = json_encode([
                    'id' => $this->id,
                    'token' => md5(json_encode([
                        'id' => $this->id,
                        'type' => $this->type,
                        'longitude' => $this->longitude,
                        'latitude' => $this->latitude,
                    ])),
                ]);

                $base64Image = (new \chillerlan\QRCode\QRCode())->render($code);

                return view('qrcode', ['code' => $base64Image]);//配置view，传入自定义参数
            });

            $grid->filter(function (Grid\Filter $filter) {
//                $filter->panel();
//                $filter->expand();
                $filter->equal('id');
                $filter->equal('name');
                $filter->equal('type')->select(\App\Models\Coin::TYPE);
            });
            $grid->model()->orderBy('id', 'desc');

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append('<a href="/admin/clues?coin_id=' . $actions->getKey() . '">线索</a>');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Coin(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('type')->using(\App\Models\Coin::TYPE);
            if ($show->model()->type == \App\Models\Coin::TYPE_JIN) {
                $show->field('is_true')->using(\App\Models\Coin::IS_TRUE);
                $show->field('amount', '橡果数');
            } else if ($show->model()->type == \App\Models\Coin::TYPE_ZM) {
                $show->field('icon', '字母');
            } else if ($show->model()->type == \App\Models\Coin::TYPE_LW) {
                $show->field('icon', '图标')->as(function ($icon) {
                    return '<img src="' . $icon . '" style="width: 100px;height: 100px;">';
                })->unescape();
            }
            $show->field('longitude');
            $show->field('latitude');
            $show->field('address', '地址');
            $show->field('status')->using(\App\Models\Coin::STATUS);
            $show->field('is_new_user_only', '新用户专享')->using(\App\Models\Coin::IS_NEW_USER_ONLY);

            $show->field('is_unlock')->using(\App\Models\Coin::IS_UNLOCK);
            $show->field('cost', '价值');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Coin(), function (Form $form) {
            $form->text('name');
            $form->radio('type')
                ->options(\App\Models\Coin::TYPE)
                ->default(\App\Models\Coin::TYPE_TONG)
                ->when(\App\Models\Coin::TYPE_JIN, function (Form $form) {
                    $form->switch('is_true');
                    $form->number('amount', '橡果数');
                })
                ->when(\App\Models\Coin::TYPE_ZM, function (Form $form) {
                    $form->select('icon_tag', '字母')
                        ->options(\App\Models\Coin::TYPE_ZM_SET)
                        ->value($form->model()->icon);
                })
                ->when(\App\Models\Coin::TYPE_LW, function (Form $form) {
                    $form->image('icon', '图标')->retainable()->removable(false)->autoUpload();
//                    if ($form->model()->icon) {
//                        $form->html('<img src="' . $form->model()->icon . '" height="200px" width="200px"/>');
//                    }
                });
            $form->text('longitude');
            $form->text('latitude');
            $form->switch('status');
            $form->switch('is_new_user_only', '新用户专享');
            $form->text('address', '地址');
            $form->decimal('cost', '价值')->value(0);
            $form->saving(function (Form $form) {
                if ($form->type != \App\Models\Coin::TYPE_JIN) {
                    $form->is_true = \App\Models\Coin::IS_TRUE_YES;
                }
                if ($form->type == \App\Models\Coin::TYPE_ZM) {
                    $form->icon = $form->icon_tag;
                }
                $form->icon = $form->icon ?: '';
                $form->deleteInput('icon_tag');
            });
        });
    }
}
